﻿// -----------------------------------------------------------------------
// <copyright file="AttributPointCut.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Raop.Library.Aop.PointCut
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Roslyn.Compilers.CSharp;
    using Raop.Library.Tools;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AttributPointCut : VisitorBasePointCut
    {
        protected override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            if (node.BodyOpt != null && node.Attributes.Count > 0)
            {
                var block = node.BodyOpt;

                var s = this.model.GetDeclaredSymbol(node);

                foreach (var att in s.GetAttributes())
                {
                    if (Inherit<BaseAopAttribute>(att.AttributeClass))
                    {
                        block = AddAop(att.AttributeClass.Name, s.ContainingType.Name, s.Name, block);
                    }
                }

                node = node.ReplaceNode(node.BodyOpt, block);
            }

            return base.VisitMethodDeclaration(node);
        }
    }
}
